package com.cc2019.buke;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.cc2019.buke.bcar.frag.BcarFragment;
import com.cc2019.buke.home.frag.HomeFragment;
import com.cc2019.buke.type.frag.TypeFragment;
import com.cc2019.buke.user.frag.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private RadioGroup radioGroup;
    private List<Fragment> frags;
    private int currentFragIndex;
    private int nowFragIndex = -1;
    private String name ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = findViewById(R.id.rgroup);
        Intent intent = this.getIntent();
        name =intent.getStringExtra("username");
        frags = initFragment();
        radioChecked();
        radioGroup.check(0);//默认选择首页


        //getSupportFragmentManager().beginTransaction().replace(R.id.radio_type,new TypeFragment()).commit();

        if (intent!=null){
            String action = intent.getAction();
            if (action!=null && "toUserInfo".equals(action)){
                radioGroup.check(R.id.radio_user);
            }
        }
    }
    public void radioChecked(){
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_index:
                        //切换不同的fragment来显示
                        currentFragIndex = 0;
                        break;
                    case R.id.radio_type:
                        currentFragIndex = 1;
                        break;
                    case R.id.radio_bcar:
                        currentFragIndex = 2;
                        break;
                    case R.id.radio_user:
                        currentFragIndex = 3;
                        break;
                }
                //切换frag显示
                if (nowFragIndex != currentFragIndex){
                    getSupportFragmentManager().beginTransaction().replace(R.id.fra_position,frags.get(currentFragIndex)).commit();
                    nowFragIndex = currentFragIndex;

                }
            }
        });
    }



    public List<Fragment> initFragment(){
        List<Fragment> frags = new ArrayList<Fragment>();
        HomeFragment homeFragment = new HomeFragment();
        TypeFragment typeFragment = new TypeFragment();
        BcarFragment bcarFragment = new BcarFragment();
        UserFragment userFragment = new UserFragment(name);
        frags.add(homeFragment);
        frags.add(typeFragment);
        frags.add(bcarFragment);
        frags.add(userFragment);
        return frags;
    }
}